/*
 * Date         Author     Note
 * 2014.11.24   舞者无罪        文件管理窗口
 * 2015.06.24   wzwz      增加返回按钮
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "file.h"
#include "com.h"
#include "file_select.h"
#include "program.h"
#include "flower.h"
#include "file_delete.h"
#include "file_copy.h"
#include "password.h"
#include "dlg_ask.h"
#include "info.h"

#define FILE_CAPTION_LEFT    (30)
#define FILE_CAPTION_TOP     (60)
#define FILE_CAPTION_WIDTH   (200)
#define FILE_CAPTION_HEIGHT  (30)
/*
 *
 */
static void CreateFileInterface(HWND hWnd)
{
	CreateWindow (MH_BTN_NAME,
        MW_FILE_SELECT1_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_BTN_FILE,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP, FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_FILE_CNT2_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_BTN_PRG,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_FILE_PAT3_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_BTN_FLW,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT * 2,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_FILE_DEL4_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_DELETE,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT * 3,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_FILE_COPY5_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_OTHER_PARAM,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT * 4,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_FILE_CLEAR6_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_CLEAR,
        FILE_CAPTION_LEFT, FILE_CAPTION_TOP + FILE_CAPTION_HEIGHT * 5,
        FILE_CAPTION_WIDTH, FILE_CAPTION_HEIGHT,
        hWnd, 0);
    char esc[64] = {0};
    sprintf(esc, GetString(802), 7);
    CreateWindow(MH_BTN_NAME,
        esc,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_ESC,
        30,
        60 + 30 * 6,
        200,
        30,
        hWnd, 0);
}
/*
 *
 */
static void ClearMemFile(HWND hWnd)
{
    HWND hDlg = CreatePasswordDialog(hWnd);
    if(PasswordDoModal(hDlg))
    {
        hDlg = CreateAskDialog(hWnd);
        SetWindowCaption(hDlg, MW_CLEAR_MEM_CAPTION);
        SetAskInfo(hDlg, MW_ASK_CLEAR_MEM_CAPTION);
        if(AskDoModal(hDlg))
        {
            char sc[64] = {0};
            sprintf(sc, "rm -R %s*", KNIT_FILE_DIR);
            system(sc);
        }
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
	switch(nID)
	{
	case ID_BTN_FILE:
	    {
	    	CreateFileSelectWindow(hWnd);
	    }
		break;
	case ID_BTN_PRG:
        {
            CreateProgramWindow(hWnd);
        }
	    break;
	case ID_BTN_FLW:
        {
            CreateFlowerWindow(hWnd);
        }
	    break;
	case ID_DELETE:
        {
            HWND hDlg = CreatePasswordDialog(hWnd);
            if(PasswordDoModal(hDlg))
            {
                CreateFileDeleteWindow(hWnd);
            }
        }
	    break;
	case ID_OTHER_PARAM:
        {
            CreateFileCopyWindow(hWnd);
        }
	    break;
	case ID_CLEAR:
        {
            ClearMemFile(hWnd);
        }
	    break;
	case ID_ESC:
	    {
	        PostMessage (hWnd, MSG_CLOSE, 0, 0);
	    }
	}
}
/*
 *
 */
static void OnMoveFocus(HWND hWnd, int up)
{
    ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
    switch(nID)
    {
    case ID_BTN_FILE:
        if(up)
        {
            //
        }
        else
        {
            nID = ID_BTN_PRG;
        }
        break;
    case ID_BTN_PRG:
        if(up)
        {
            nID = ID_BTN_FILE;
        }
        else
        {
            nID = ID_BTN_FLW;
        }
        break;
    case ID_BTN_FLW:
        if(up)
        {
            nID = ID_BTN_PRG;
        }
        else
        {
            nID = ID_DELETE;
        }
        break;
    case ID_DELETE:
        if(up)
        {
            nID = ID_BTN_FLW;
        }
        else
        {
            nID = ID_OTHER_PARAM;
        }
        break;
    case ID_OTHER_PARAM:
        if(up)
        {
            nID = ID_DELETE;
        }
        else
        {
            nID = ID_CLEAR;
        }
        break;
    case ID_CLEAR:
        if(up)
        {
            nID = ID_OTHER_PARAM;
        }
        else
        {
            nID = ID_ESC;
        }
        break;
    case ID_ESC:
        if(up)
        {
            nID = ID_CLEAR;
        }
        break;
    }
    SetFocusChild(GetDlgItem(hWnd, nID));
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
	if(key == SCANCODE_ESCAPE || key == SCANCODE_7)
	{
		OnBtnCmd(hWnd, ID_ESC);
	}
	else if(key == SCANCODE_1)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_BTN_FILE));
	    OnBtnCmd(hWnd, ID_BTN_FILE);
	}
	else if(key == SCANCODE_2)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_BTN_PRG));
	    OnBtnCmd(hWnd, ID_BTN_PRG);
	}
	else if(key == SCANCODE_3)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_BTN_FLW));
	    OnBtnCmd(hWnd, ID_BTN_FLW);
	}
	else if(key == SCANCODE_4)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_DELETE));
	    OnBtnCmd(hWnd, ID_DELETE);
	}
	else if(key == SCANCODE_5)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_OTHER_PARAM));
	    OnBtnCmd(hWnd, ID_OTHER_PARAM);
	}
	else if(key == SCANCODE_6)
	{
	    SetFocusChild(GetDlgItem(hWnd, ID_CLEAR));
	    OnBtnCmd(hWnd, ID_CLEAR);
	}
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        OnMoveFocus(hWnd, FALSE);
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        OnMoveFocus(hWnd, TRUE);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
}
/*
 *
 */
static int FileWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
    {
    case MSG_CREATE:
        {
        	CreateFileInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_MAST]);
        }
    	return 0;
    case MSG_COMMAND:
        {
        	OnBtnCmd(hWnd, LOWORD(wParam));
        }
    	return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateFileWindow(HWND hHosting)
{
    int x = (CX_SCREEN - MASTER_WND_CX) / 2;
    int y = (CY_SCREEN - MASTER_WND_CY) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_FILE_BT_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = FileWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + MASTER_WND_CX;
    CreateInfo.by = y + MASTER_WND_CY;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_BTN_FILE));
    	ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
